package com.zsh.rabbitmq.six;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zsh.rabbitmq.utils.RabbitMQUtils;

/**
 * @author zsh
 * @Description 直连模式
 * @create 2022-05-05 18:40
 */
public class ReceiveLogsDirect02 {

    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();

        //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //声明队列  临时队列
        /*
            生成一个临时队列，队列名称是随机的
            当消费者断开队列链接时，队列自动删除
         */
        String queueName = "disk";
        channel.queueDeclare(queueName,false,false,false,null);

        //绑定交换机与队列
        channel.queueBind(queueName,EXCHANGE_NAME,"error");
        System.out.println("ReceiveLogsDirect02 等待接收消息......");

        //接收消息
        DeliverCallback deliverCallback = (consumerTag, message) ->{
            String msg = new String(message.getBody(),"UTF-8");
            System.out.println("ReceiveLogsDirect02 接收到的消息是：" + msg);
        };

        //取消消息,先不写

        channel.basicConsume(queueName,true,deliverCallback, (CancelCallback) null);
    }


}
